/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using ELE.EnterpriseLibrary.Naming.Configuration;
using ELE.EnterpriseLibrary.Properties;

namespace ELE.EnterpriseLibrary.Naming
{
  /// <summary>
  /// A view of object storage configuration data.
  /// </summary>
  public class NamingContextConfigurationView
  {
    private IConfigurationSource configurationSource;
    /// <summary>
    /// Initializes the view from a configuration source.
    /// </summary>
    /// <param name="configuration"></param>
    public NamingContextConfigurationView(IConfigurationSource configuration)
    {
      this.configurationSource = configuration;
    }
    /// <summary>
    /// Get storage data by name.
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public NamingContextData GetContextData(string name)
    {
      NamingContextSettings settings = this.ContextSettings;
      if (!settings.Contexts.Contains(name))
      {
        throw new ConfigurationErrorsException(string.Format(Resources.Culture, Resources.ExceptionContextNotFound, name));
      }
      return settings.Contexts.Get(name);
    }
    /// <summary>
    /// Gets the name of the default <see cref="NamingContextData"/>.
    /// </summary>
    /// <returns>
    /// The name of the default <see cref="NamingContextData"/>.
    /// </returns>
    public string DefaultContext
    {
      get
      {
        NamingContextSettings configSettings = this.ContextSettings;
        return configSettings.DefaultContext;
      }
    }

    /// <summary>
    /// Gets the <see cref="NamingContextSettings"/> configuration data.
    /// </summary>
    /// <returns>
    /// The <see cref="NamingContextSettings"/> configuration data.
    /// </returns>
    public NamingContextSettings ContextSettings
    {
      get { return (NamingContextSettings)configurationSource.GetSection(NamingContextSettings.SectionName); }
    }
  }
}
